import { PerspectiveCamera, WebGLRenderer } from 'three';
const setSize = (container: HTMLElement, camera: PerspectiveCamera, renderer: WebGLRenderer) => {
  // Set the camera's aspect ratio
  camera.aspect = container.clientWidth / container.clientHeight;

  // 相机使用纵横比以及视野以及近远裁剪平面来计算它的 视锥。
  // 平截头体不会自动重新计算，
  // 因此当我们更改存储在camera.aspect、camera.fov、camera.near和camera.far中的任何这些设置时，
  // 我们还需要更新平截头体。updateProjectionMatrix
  // update the camera's frustum
  camera.updateProjectionMatrix();

  // update the size of the renderer AND the canvas
  renderer.setSize(container.clientWidth, container.clientHeight);

  // set the pixel ratio (for mobile devices)
  renderer.setPixelRatio(window.devicePixelRatio);
};
class Resizer {
  resizeEvents: any[] = [];
  constructor(container: HTMLElement, camera: PerspectiveCamera, renderer: WebGLRenderer) {
    // set initial size
    setSize(container, camera, renderer);

    const fn = () => {
      // set the size again if a resize occurs
      setSize(container, camera, renderer);

      // perform any custom actions
      this.onResize();
    };
    
    this.resizeEvents.push(fn);
    window.addEventListener('resize', fn);
  }
  onResize() {}
  dispose() {
    const { resizeEvents } = this;
    resizeEvents.forEach((fn) => window.removeEventListener('resize', fn));
  }
}

export { Resizer };
